package com.pkk.blny.model.tag;

import com.pkk.blny.model.enums.PostStatusEnum;
import com.pkk.blny.model.enums.PostTypeEnum;
import com.pkk.blny.service.PostService;
import com.pkk.blny.model.enums.PostStatusEnum;
import com.pkk.blny.model.enums.PostTypeEnum;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <pre>
 *     FreeMarker自定义标签
 * </pre>
 *
 * @author : RYAN0UP
 * @date : 2018/4/26
 */
@Component
public class ArticleTagDirective implements TemplateDirectiveModel {

  private static final String METHOD_KEY = "method";

  @Autowired
  private PostService postService;

  @Override
  public void execute(Environment environment, Map map, TemplateModel[] templateModels,
      TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
    DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
    if (map.containsKey(METHOD_KEY)) {
      String method = map.get(METHOD_KEY).toString();
      switch (method) {
        case "postsCount":
          environment.setVariable("postsCount", builder.build().wrap(
              postService.findPostByStatus(PostStatusEnum.PUBLISHED.getCode(), PostTypeEnum.POST_TYPE_POST.getDesc())
                  .size()));
          break;
        case "archives":
          environment.setVariable("archives", builder.build().wrap(postService.findPostGroupByYearAndMonth()));
          break;
        case "archivesLess":
          environment.setVariable("archivesLess", builder.build().wrap(postService.findPostGroupByYear()));
          break;
        case "hotPosts":
          environment.setVariable("hotPosts", builder.build().wrap(postService.hotPosts()));
          break;
        default:
          break;
      }
    }
    templateDirectiveBody.render(environment.getOut());
  }
}
